<script type="text/javascript">
    $(document).ready(function() {
        document.title = "easyGIS - Aplicações: Filtering";
		new AjaxUpload($('#shape_map'), {
			action: 'UploadShapeMap.php',
			name:	'file_shape_map',
			onSubmit: function(file, extension) {
				if (! (extension && /^(txt|xml|json)$/.test(extension))){
					$('<p class="message_error"></p>').appendTo($('#message_shape_map_upload')).text('São permitidas apenas as extensões TXT, XML e JSON.');
					return false;
				}
				if($('#message_shape_map_upload .message_error').length != 0)
					$('#message_shape_map_upload .message_error').hide();
				$('#upload_shape_map').toggle("slow");
				$('#status_shape_map_upload').text('Carregando...');
			},
			onComplete : function(file, response) {
				$('#status_shape_map_upload').hide();
				if(response == 'success') {
					$('<p class="message_success"></p>').appendTo($('#message_shape_map_upload')).text(file);
				}
				if(response == 'error') {
					$('<p class="message_error"></p>').appendTo($('#message_shape_map_upload')).text("Erro durante o upload do arquivo " . file);
				}
			}
		});

		new AjaxUpload($('#original_productivity_map'), {
			action: 'UploadOriginalProductivityMap.php',
			name:	'file_original_productivity_map',
			onSubmit: function(file, extension) {
				if (! (extension && /^(txt|xml|json)$/.test(extension))){
					$('<p class="message_error"></p>').appendTo($('#message_original_productivity_map_upload')).text('São permitidas apenas as extensões TXT, XML e JSON.');
					return false;
				}
				if($('#message_original_productivity_map_upload .message_error').length != 0)
					$('#message_original_productivity_map_upload .message_error').hide();
				$('#upload_original_productivity_map').toggle("slow");
				$('#status_original_productivity_map_upload').text('Carregando...');
			},
			onComplete : function(file, response) {
				$('#status_original_productivity_map_upload').hide();
				if(response == 'success') {
					$('<p class="message_success"></p>').appendTo($('#message_original_productivity_map_upload')).text(file);
				}
				if(response == 'error') {
					$('<p class="message_error"></p>').appendTo($('#message_original_productivity_map_upload')).text("Erro durante o upload do arquivo " . file);
				}
			}
		});
			//}
			//return false;
		});
		
		$('#submit_filtering').click(function() {
			if(validateInputFiles()) {
				$.ajax({
					type:   'POST',
					url:    "FilteringCore.php",
					data:   {				 file_shape_map: $('#message_shape_map_upload .message_success').text(),
							 file_original_productivity_map: $('#message_original_productivity_map_upload .message_success').text()
					},
					beforeSend: function(){
						$('#content').show();
						$('#content').html('<div id="loading">Carregando...</div>');
					},
					success: function(response){
						$('#header').hide();
						$('#wrapper').hide();
						$('#footer').hide();
						$('#application').html(response);
					},
					error: function(message){
						$('#content').html(message);
					}
				});
			}
			else
				return false;
		});

    function validateInputFiles()
    {
        if(isNullOrEmpty($('#message_shape_map_upload .message_success').text()))
        {
            alert("Favor informar um mapa de contorno de talhão.");
            return false;
        }
        if(isNullOrEmpty($('#message_original_productivity_map_upload .message_success').text()))
        {
            alert("Favor informar um mapa de produtividade.");
            return false;
        }
        return true;
    }
</script>

<h1 class="title"><a id="filtering_algorithm" href="#">Algoritmo <i>Filtering</i></a></h1>
<div class="post">
	<p class="bysolidline"><small>&nbsp;</small></p>
	<div class="upload">
		<form action="Home.php" method="post" enctype="multipart/form-data">
			<p id="upload_shape_map">
				<label for="shape_map">Mapa de Contorno:</label>
				<input id="shape_map" type="file" size="40" />
			</p>
			<span id="status_shape_map_upload" ></span>
			<div id="message_shape_map_upload"></div>

			<p id="upload_original_productivity_map">
				<label for="original_productivity_map">Mapa de Produtividade:</label>
				<input id="original_productivity_map" type="file" size="40" />
			</p>
			<span id="status_original_productivity_map_upload"></span>
			<div id="message_original_productivity_map_upload"></div>

			<p>
				<input id="submit_filtering" type="submit" value="Iniciar Filtering" />
			</p>
		</form>
	</div>
	<div id="example3"></div>
</div>